﻿@using PagedList.Mvc;
@using PagedList;
@using TestOnlineProject.Models.Entities;
@using TestOnlineProject.Common;
<script>
    $(document).ready(function () {
        // Select all.
        $("#select_all").click(function () {
            $(".is_selected").attr('checked', true);
        });

        // Unselect all.
        $("#unselect_all").click(function () {
            $(".is_selected").attr('checked', false);
        });

        $("#deactivate").click(function () {
            var con = confirm("Are you sure you want to mark as unread this report(s)?");
            if (con == true) {
                var val = "";
                $(':checkbox:checked').each(function (i) {
                    val += $(this).val() + "-";
                });
                if (val == "") {
                    alert("You must select at least one report!")
                }
                else {
                    // Uncheck.
                    $.ajax({
                        url: "Report/Uncheck?ids=" + val,
                        type: 'GET',
                        success: function (data) {
                            if (data != null) {
                                alert(data);
                                // Reload page.
                                $.ajax({
                                    url: "Report/Index?page=@ViewBag.Page&keyWord=@ViewBag.KeyWord&column=@ViewBag.Column&sort=@ViewBag.Sort",
                                    type: 'GET',
                                    success: function (data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#activate").click(function () {
            var con = confirm("Are you sure you want to mark as read this report(s)?");
            if (con == true) {
                var val = "";
                $(':checkbox:checked').each(function (i) {
                    val += $(this).val() + "-";
                });
                if (val == "") {
                    alert("You must select at least one report.")
                }
                else {
                    // Uncheck.
                    $.ajax({
                        url: "Report/Check?ids=" + val,
                        type: 'GET',
                        success: function (data) {
                            if (data != null) {
                                alert(data);
                                // Reload page.
                                $.ajax({
                                    url: "Report/Index?page=@ViewBag.Page&keyWord=@ViewBag.KeyWord&column=@ViewBag.Column&sort=@ViewBag.Sort",
                                    type: 'GET',
                                    success: function (data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#delete").click(function () {
            var con = confirm("Are you sure you want to delete this report(s)?");
            if (con == true) {
                var val = "";
                $(':checkbox:checked').each(function (i) {
                    val += $(this).val() + "-";
                });
                if (val == "") {
                    alert("You must select at least one report.")
                }
                else {
                    // Uncheck.
                    $.ajax({
                        url: "Report/Delete?ids=" + val,
                        type: 'GET',
                        success: function (data) {
                            if (data != null) {
                                alert(data);
                                // Reload page.
                                $.ajax({
                                    url: "Report/Index?page=@ViewBag.Page&keyWord=@ViewBag.KeyWord&column=@ViewBag.Column&sort=@ViewBag.Sort",
                                    type: 'GET',
                                    success: function (data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

    });
</script>
<div id="list">
    <div>
        <table class="table table-hover" style="table-layout: fixed; width: 100%">
            <tr>
                <th style="vertical-align: middle; width: 50px">
                    @Ajax.ActionLink("No.", "Index", new { column = "No", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "No") ? "Desc" : "Asc", keyWord = @ViewBag.KeyWord }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                <th style="vertical-align: middle">
                    @Ajax.ActionLink("Message", "Index", new { column = "Message", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "Message") ? "Desc" : "Asc", keyWord = @ViewBag.KeyWord }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                @* <th style="vertical-align: middle; width: 60px">
                    @Ajax.ActionLink("Checked", "Index", new { column = "IsChecked", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "IsChecked") ? "Desc" : "Asc", keyWord = @ViewBag.KeyWord }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>*@
                <th style="width: 30px">
                    <div class="btn-group">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu pull-right">
                            <li>
                                <button class="btn btn-link" id="select_all">
                                    Select All</button></li>
                            <li>
                                <button class="btn btn-link" id="unselect_all">
                                    Unselect All</button></li>
                            <li>
                                <button class="btn btn-link" id="deactivate">
                                    Mark as unread</button></li>
                            <li>
                                <button class="btn btn-link" id="activate">
                                    Mark as read</button></li>
                            <li>
                                <button class="btn btn-link" id="delete">
                                    Delete</button></li>
                        </ul>
                    </div>
                </th>
            </tr>
            @{
                int row = 0;
                foreach (var report in ViewBag.onePageOfReports as IEnumerable<Report>)
                {
                    row++;
                <tr id="row_@row">
                    <td>
                        @{
                    var noList = ViewBag.NoList as List<NoModel>;
                    var no = noList.Where(n => n.Id == report.ReportId).FirstOrDefault().No;
                            <span style="word-wrap: break-word">@no</span>
                        }
                    </td>
                    <td>
                        @{
                            <span style="word-wrap: break-word">@Html.ActionLink(report.Message, "Details", new { id = report.ReportId, isChecked = "checked" }, null)</span>
                        }
                    </td>
                    @*<td>
                        @{
                    if (report.IsChecked)
                    {
                            <span>Yes</span>
                    }
                    else
                    {
                            <span>No</span>
                    }
                        }
                    </td>*@
                    <td>
                        <input type="checkbox" value="@report.ReportId" class="is_selected"/>
                    </td>
                </tr>
                    if (!report.IsChecked)
                    {
                <script>                    $("#row_@row").addClass("uncheck"); </script>
                    }
                }
            }
        </table>
    </div>
    @Html.PagedListPager((IPagedList)ViewBag.onePageOfReports, page => Url.Action("Index", new { page, sort = @ViewBag.Sort, column = @ViewBag.Column, keyWord = @ViewBag.KeyWord }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "unobtrusive", OnComplete = "" }))
</div>
